package com.example.socket.controller;

import com.example.socket.common.ResultMap;
import com.example.socket.common.ValidatorUtils;
import com.example.socket.model.PageUtils;
import com.example.socket.model.ScheduleJobEntity;
import com.example.socket.service.IScheduleJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created with IDEA
 * author:QinWei
 * Date:2019/5/20
 * Time:14:37
 */
@Api(value = "定时任务", tags = {" 定时任务"})
@RestController
@RequestMapping("/sys/schedule")
public class QuartzApiController {


    @Autowired
    private IScheduleJobService scheduleJobService;

    /**
     * 定时任务列表
     */
    @ApiOperation(value = "定时任务列表", notes = "定时任务列表")
    @GetMapping("/list")
    public ResultMap list(@RequestParam Map<String, Object> params) {
        PageUtils page = scheduleJobService.queryPage(params);

        return ResultMap.ok().put("page", page);
    }

    /**
     * 定时任务信息
     */
    @ApiOperation(value = "定时任务信息", notes = "定时任务信息")
    @GetMapping("/info/{jobId}")
    public ResultMap info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity schedule = scheduleJobService.getById(jobId);

        return ResultMap.ok().put("schedule", schedule);
    }

    /**
     * 保存定时任务
     */
    @ApiOperation(value = "保存定时任务", notes = "保存定时任务")
    @PostMapping("/save")
    public ResultMap save(@RequestBody ScheduleJobEntity scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);

        scheduleJobService.insert(scheduleJob);

        return ResultMap.ok();
    }

    /**
     * 修改定时任务
     */
    @ApiOperation(value = "修改定时任务", notes = "修改定时任务")
    @PostMapping("/update")
    public ResultMap update(@RequestBody ScheduleJobEntity scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);

        scheduleJobService.update(scheduleJob);

        return ResultMap.ok();
    }

    /**
     * 删除定时任务
     */
    @ApiOperation(value = "删除定时任务", notes = "删除定时任务")
    @PostMapping("/delete")
    public ResultMap delete(@RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);

        return ResultMap.ok();
    }

    /**
     * 立即执行任务
     */
    @ApiOperation(value = "立即执行任务", notes = "立即执行任务")
    @PostMapping("/run")
    public ResultMap run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);

        return ResultMap.ok();
    }

    /**
     * 暂停定时任务
     */
    @ApiOperation(value = "暂停定时任务", notes = "暂停定时任务")
    @PostMapping("/pause")
    public ResultMap pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);

        return ResultMap.ok();
    }

    /**
     * 恢复定时任务
     */
    @ApiOperation(value = "恢复定时任务", notes = "恢复定时任务")
    @PostMapping("/resume")
    public ResultMap resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);

        return ResultMap.ok();
    }
}
